<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "bid_queue".
 *
 * @property integer $bidsericesqueueid
 * @property integer $product_id
 * @property integer $auction_id
 * @property integer $user_id
 * @property string $img
 * @property string $name
 * @property string $province
 * @property string $city
 * @property string $county
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $num
 * @property integer $initial_num
 * @property User $user
 */
class BidSericesqueue extends ActiveRecord
{
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes'=>[
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'bid_sericesqueue';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['product_id', 'auction_id', 'user_id', 'img', 'name', 'province', 'city', 'county','num'], 'required'],
            [['product_id', 'auction_id', 'user_id', 'state'], 'integer'],
            [['price'], 'number'],
            [['img', 'name', 'province', 'city', 'county'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'bbidsericesqueueid' => 'ID',
            'product_id' => '产品id',
            'auction_id' => '竞拍id',
            'user_id' => '用户id',
            'img' => '头像',
            'name' => '用户昵称',
            'province' => '省',
            'city' => '市',
            'county' => '区',
            'state' => '状态',
            'price' => '成交价',
            'num'=>'出价次数',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }

    public static function create(Auction $auction,User $user,$num)
    {
        //保持连续
        /** @var  $oldModel self*/
        $oldModel = self::find()->where(['auction_id'=>$auction->auctionid,'user_id'=>$user->userid])->one();
        if($oldModel){
            $oldModel->num+=$num;
            $oldModel->initial_num+=$num;
            $oldModel->save(false);
            return $oldModel;
        }

        $model = new self();
        $model->product_id = $auction->product_id;
        $model->auction_id = $auction->auctionid;
        $model->user_id = $user->userid;
        $model->img = $user->img;
        $model->name = $user->name;
        $model->province = $user->province;
        $model->city = $user->city;
        $model->num = $num;
        $model->initial_num = $num;
        $model->save(false);
        return $model;
    }

    public function getUser()
    {
        return $this->hasOne(User::className(),['userid'=>'user_id']);
    }

    public function getAuction()
    {
        return $this->hasOne(Auction::className(),['auctionid'=>'auction_id']);
    }

    public function getProduct()
    {
        return $this->hasOne(Product::className(),['pid'=>'product_id']);
    }
}